package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysDeptCamera;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysDeptCameraMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysDeptCameraService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 部门-摄像头
 *
 * @author miracle_j
 * @date 2022年11月21日 16:28:00
 */
@Service
public class SysDeptCameraServiceImpl implements ISysDeptCameraService {
    @Autowired
    private ISysDeptCameraMapper sysDeptCameraMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(SysDeptCamera req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysDeptCamera req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysDeptCameraMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysDeptCamera sysDeptCamera) {
        WJAssert.notNull(sysDeptCamera, "入参对象不能为空");
        WJAssert.notNull(sysDeptCamera.getId(), "id参数错误");
        SysDeptCamera where = new SysDeptCamera();
        where.setId(sysDeptCamera.getId());
        sysDeptCamera.setUpdateTime(new Date());
        return sysDeptCameraMapper.updateSelective(sysDeptCamera, where);
    }

    @Override
    public int getCount(SysDeptCamera sysDeptCamera) {
        checkStringLength(sysDeptCamera, true);
        return sysDeptCameraMapper.getCount(sysDeptCamera);
    }

    @Override
    public SysDeptCamera getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysDeptCamera where = new SysDeptCamera();
        where.setId(id);
        return sysDeptCameraMapper.getOne(where);
    }

    @Override
    public List<SysDeptCamera> getList(SysDeptCamera sysDeptCamera) {
        checkStringLength(sysDeptCamera, true);
        return sysDeptCameraMapper.getList(sysDeptCamera);
    }

    @Override
    public List<SysDeptCamera> getPageList(SysDeptCamera sysDeptCamera) {
        checkStringLength(sysDeptCamera, true);
        return sysDeptCameraMapper.getPageList(sysDeptCamera);
    }

    @Override
    public void audit(Long id, String operator) {
        SysDeptCamera req = new SysDeptCamera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysDeptCamera req = new SysDeptCamera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysDeptCamera req = new SysDeptCamera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public SysDeptCamera getOneByName(String name) {
        return sysDeptCameraMapper.getOneByName(name);
    }

    @Override
    public SysDeptCamera getOneByDeviceId(Long deviceId) {
        return sysDeptCameraMapper.getOneByDeviceId(deviceId);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysDeptCamera sysDeptCamera, boolean nullAble) {
        WJAssert.notNull(sysDeptCamera, "入参对象不能为空");
        WJAssert.limitMaxLength(sysDeptCamera.getName(), nullAble, 70, "摄像头名称不合法!");
        WJAssert.limitMaxLength(sysDeptCamera.getDepName(), nullAble, 50, "部门名称不合法!");
        WJAssert.limitMaxLength(sysDeptCamera.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysDeptCamera.getRemark(), true, 255, "备注不合法!");
    }
}
